package example.demo;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.Exchanger;

@Slf4j
//关闭quartz启动器，省的每次启动很慢
@SpringBootApplication(exclude = {QuartzAutoConfiguration.class})
@MapperScan("example.demo.mapper")
@EnableAspectJAutoProxy(exposeProxy = true)
public class QuartzSimpleUseApp {
    public static Exchanger<List<String>> listExchanger = new Exchanger<>();

    public static void main(String[] args) throws Exception {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        new Thread(() -> {
            try {
                log.info("准备收集----------------------Collect-Thread" + dateFormat.format(new Date()));
                List<String> urls = listExchanger.exchange(null);//wait-thread
                log.info("时间---【{}】----收集完成的url--{}", dateFormat.format(new Date()), urls.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        // 类加载器的classes有点像spring父子容器关系,还有springboot与spring-cloud再openfeign的容器的关系
        SpringApplication.run(QuartzSimpleUseApp.class);
        ClassLoader app = Thread.currentThread().getContextClassLoader();
        Class<ClassLoader> classLoaderClass = ClassLoader.class;
        Field classesField = classLoaderClass.getDeclaredField("classes");
        classesField.setAccessible(true);
        Vector<Class> appclasses = (Vector) classesField.get(app);
        appclasses.stream().map(Class::getName).map(name -> "app加载的类==" + name).forEach(System.out::println);

        System.out.println("==========================================================");
        Field parent = classLoaderClass.getDeclaredField("parent");
        parent.setAccessible(true);
        ClassLoader ext = (ClassLoader) parent.get(app);
        Vector<Class> extclasses = (Vector<Class>) classesField.get(ext);
        extclasses.stream().map(Class::getName).map(name -> "ext加载的类==" + name).forEach(System.out::println);
        System.out.println();

    }
}
